package com.itheima.controller;

import Jama.Matrix;

import com.itheima.until.LinearAlgebra;
import com.itheima.until.Result;
import com.itheima.until.twoDArrays;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Controller
@RequestMapping("/math")
public class MathController {


    @RequestMapping("/determinant")
    @ResponseBody
    public Result determinant(@RequestBody twoDArrays twoDArray){
        System.out.println("twoDArrays = " + twoDArray);
        double[][] A = twoDArray.getTwoDArray1();
        LinearAlgebra linearAlgebra = new LinearAlgebra(A);

        double value = linearAlgebra.determinant();

        return new Result(101,value);
    }

    @RequestMapping("/solveLinearEquations")
    @ResponseBody
    public Result solveLinearEquations(@RequestBody twoDArrays twoDArray){
        System.out.println("twoDArray1 = " + twoDArray);
        double[][] A = twoDArray.getTwoDArray1();
        double[][] B = twoDArray.getTwoDArray2();
        LinearAlgebra linearAlgebra = new LinearAlgebra(A);
        Matrix matrix = new Matrix(B);

        Matrix matrix1 = linearAlgebra.solveLinearEquations(matrix);
        double[][] array = matrix1.getArray();
        return new Result(101,array);
    }
    @RequestMapping("/addMatrices")
    @ResponseBody
    public Result addMatrices(@RequestBody twoDArrays twoDArray){
        System.out.println("twoDArray = " + twoDArray);
        double[][] A = twoDArray.getTwoDArray1();
        double[][] B = twoDArray.getTwoDArray2();
        LinearAlgebra linearAlgebra = new LinearAlgebra(A);
        Matrix matrix = new Matrix(B);

        Matrix matrix1 = linearAlgebra.addMatrices(matrix);

        return new Result(101,matrix1.getArray());
    }

    @RequestMapping("/multiplyMatrices")
    @ResponseBody
    public Result multiplyMatrices(@RequestBody twoDArrays twoDArray){
        double[][] A = twoDArray.getTwoDArray1();
        double[][] B = twoDArray.getTwoDArray2();
        LinearAlgebra linearAlgebra = new LinearAlgebra(A);
        Matrix matrix = new Matrix(B);

        Matrix matrix1 = linearAlgebra.multiplyMatrices(matrix);

        return new Result(101,matrix1.getArray());
    }

    @RequestMapping("/transposeMatrix")
    @ResponseBody
    public Result transposeMatrix(@RequestBody twoDArrays twoDArray){

        double[][] A = twoDArray.getTwoDArray1();
        LinearAlgebra linearAlgebra = new LinearAlgebra(A);

        Matrix matrix = linearAlgebra.transposeMatrix();

        return new Result(101,matrix.getArray());
    }

    @RequestMapping("/inverseMatrix")
    @ResponseBody
    public Result inverseMatrix(@RequestBody twoDArrays twoDArray){

        double[][] A = twoDArray.getTwoDArray1();
        LinearAlgebra linearAlgebra = new LinearAlgebra(A);

        Matrix matrix = linearAlgebra.inverseMatrix();

        return new Result(101,matrix.getArray());
    }

    @RequestMapping("/rankOfMatrix")
    @ResponseBody
    public Result rankOfMatrix(@RequestBody twoDArrays twoDArray){

        double[][] A = twoDArray.getTwoDArray1();
        LinearAlgebra linearAlgebra = new LinearAlgebra(A);

        int value = linearAlgebra.rankOfMatrix();

        return new Result(101,value);
    }
}
